<?php
defined('BY_JYA') or exit('error'); 
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$agentset=core_setting('agent');
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array();
	if($_RQ['name']){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=trim($_RQ['name']);
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=trim($_RQ['status']);
	}
    $fields=array('uniacid','name','status','commission','incode');
    $orderby='uniacid desc';
	$total='';
	$return['list']=pdo_getslice('core_agent',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($return['list']){
		foreach($return['list'] as &$item){
			$item['nickname']=pdo_getcolumn('core_users',array('uniacid'=>$item['uniacid']),'nickname');
			$item['commission']=$agentset['commission'];
		}
	}
	
	$return['check']=array('0'=>'待审核','1'=>'审核通过','2'=>'禁止');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'代理商管理'),
		
		),
		'list'=>array('data'=>'list','key'=>'uniacid','edit'=>false,'list'=>array(
			array('name'=>'name','title'=>'代理商名称','type'=>'string'),
			array('name'=>'nickname','title'=>'代理商昵称','type'=>'string'),
			array('name'=>'commission','title'=>'佣金比例%','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'incode','title'=>'邀请码','type'=>'string'),
			
		),'action'=>array(
			
			array('type'=>'modal','op'=>'check','title'=>'审核'),
			array('type'=>'modal','op'=>'commission','title'=>'佣金设置'),		
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'审核状态','type'=>'radio','options'=>'check'),
			array('name'=>'name','width'=>15,'title'=>'代理商名称','type'=>'string'),
		)
	);
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'禁止');
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='check'){

	$id=intval($_RQ['uniacid']);
	
	if($id){
	$item=pdo_get('core_agent',array('uniacid'=>$id));
	}else{
		exi('错误id','error');
	}
    $return['item']=$item;
	$return['check']=array('1'=>'通过','2'=>'禁止');
	$htmls=array(
		'type'=>'edit',
		'post'=>'check2',
		'desc'=>'代理商审核',
		'edit'=>array(
			'1'=>array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'check'),
			)
			);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check2'){
	$postdata=get_postdata();
	$data=array_fields(array('status'),$postdata);
	$id=intval($postdata['uniacid']);
	$data['checktime']=TIMESTAMP;
	if($id){
		pdo_update('core_agent',$data,array('uniacid'=>$id));
		exi('设置成功！');
	}
}
if($_RQ['op']=='commission'){
	$id=intval($_RQ['uniacid']);
	if($id){
		$item=pdo_get('core_agent',array('uniacid'=>$id));
	}else{
		exi('错误id','error');
	}
    $return['item']=$item;
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'commission2',
		'desc'=>'代理商审核',
		'edit'=>array(
			array('name'=>'commission','title'=>'佣金比例','type'=>'string','help'=>'填写百分数，例如20表示佣金比例为20%'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='commission2'){
	$postdata=get_postdata();
	$data=array_fields(array('commission'),$postdata);
	$id=intval($postdata['uniacid']);
	if($id){
		pdo_update('core_agent',$data,array('uniacid'=>$id));
		exi('设置成功！');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['uniacid']);
	if($id){
		pdo_delete('core_agent',array('uniacid'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_agent',array('uniacid'=>$id));
			}
		}
	}
	exi('删除成功！');
	
}
?>